<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class sortController extends Controller
{   
	public function menu()
	{
      //左表查询
//select  c1.*,(c2.name )as cname , concat(c1.path,',',c1.id) as asPath from goods as c1 left join goods as c2 on c1.pid = c2.id order by asPath;

     	$data = DB::table('sort as c1') 
     	     -> select('c1.*','c2.name as cname',DB::raw("concat(c1.path,',',c1.id) as aspath"))
             -> LeftJoin('sort as c2','c1.pid','=','c2.id')
             -> orderBy('aspath','ASC')
     	     -> get();
     	
        foreach($data as $k => $value)
        {
        	$count = substr_count($value -> path,',');
        	  $str = str_repeat('|----',$count);
        	$data[$k] -> cname = $str.$value -> cname;
        	if(empty($data[$k] -> cname))
        	{
        		$data[$k] -> cname = '根分类';
        	}
        }
             //dd($data);
    	return view('admin.sort.menu',['data' => $data]);
    }
   
    //修改页面************************************************
      public function alter($id)
    {    
    	 //查询当前分类
    	$data = DB::table('sort') -> where('id',$id) -> first();
    	 //查询所有分类
    	$allData = DB::table('sort')
    	     -> select(['id','name','path',DB::raw('concat(path, "," ,id)as str' )])
    	     -> orderBy('str','ASC')
             ->get();
     //dd($data);
        foreach($allData as $k => $value)
        {
        	$count = substr_count($value -> path,',');
        	$str = str_repeat('|----',$count);
        	$allData[$k] -> name = $str.$value -> name;
        }

    	//dd($allData);
    	return view('admin.sort.alter',['data' => $data,'allData' => $allData]);

    }
    //修改执行 **************************
         
       public function update(Request $request)
       {
       	 $data = $request -> except('_token');
          
          $parentPath = DB::table('sort')
                      -> where('id',$data['pid'])
                      -> first()
                      -> path;
          $data['path'] = $parentPath.','.$data['pid'];
       	  //dd($data);
       	   $res = DB::table('sort')
               -> where('id',$data['id'])
               -> update($data);
           if($res)
           {
           	 return redirect('admin/sort/menu') -> with(['info' => '修改成功']);
           }else{
             return back() -> with(['info' => '修改失败']);
           }
       	// $res = DB::table('sort') -> where('id',$request -> input('id')) -> update();
       }
       
        public function add()
    {
    	 //select id,name,path,concat(path,',',id) as str from goods order by str asc 
    	
    	$data = DB::table('sort')
    	     -> select(['id','name','path',DB::raw('concat(path, "," ,id)as str' )])
    	     -> orderBy('str','ASC')
             ->get();
     //dd($data);
        foreach($data as $k => $value)
        {
        	$count = substr_count($value -> path,',');
        	$str = str_repeat('|----',$count);
        	$data[$k] -> name = $str.$value -> name;
        }

    	return view('admin.sort.add',['data' => $data]);
    }
    //分类添加执行***************************************
    
    public function insert(Request $request)
    {
    	  //dd($request -> all());
    	  $data = $request -> except('_token');
    	  //处理字段
    	  if($request -> input('pid') == 0)
    	  {
            $data['path'] = 0;
    	  }else{
    	    $path = DB::table('sort') 
    	        -> where('id',$request -> input('pid'))
    	        -> first()
    	        -> path;
    	        //dd($path);
    	     $data['path'] = $path.','.$request -> input('pid');
           
          }
        //添加数据
          $res = DB::table('sort')
            -> insert($data);
            if($res)
            {
            	return redirect('admin/sort/menu') -> with(['info' => '添加成功']);
            }else{
            	return back() -> with(['info' => '添加失败']);
            }
    }
    public function delete($id)
    {
        $res = DB::table('sort') -> where('pid',$id) -> get();
        if($res)
        {
        	return back() -> with(['info' => '有子分类']);
        }else{
        	$row = DB::table('sort') -> where('id',$id) -> delete();
        	if($row)
        	{
        		return redirect('admin/sort/menu') -> with(['info' => '删除陈宫']);
        	}else{
        		return back() -> with(['info' => '删除失败']);
        	}
        }
    }

}
